package com.feri.buy.auth.filter;

import com.feri.buy.common.anno.CheckParam;
import com.feri.buy.common.util.StringUtils;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.lang.reflect.Method;
import java.lang.reflect.Parameter;
import java.util.Map;

/**
 * 拦截请求，验证是否需要校验参数*/
public class ParamInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        //验证当前请求是否为映射方法
        if(handler instanceof HandlerMethod) {
            HandlerMethod handlerMethod = (HandlerMethod) handler;
            //获取当前请求的方法对象
            Method method = handlerMethod.getMethod();
            //获取是否包含自定义注解
            CheckParam checkParam = method.getAnnotation(CheckParam.class);
            //获取方法的参数 -- 键值对参数
            Map<String,String[]> map=request.getParameterMap();
            for(String p : map.keySet()){
                if(!StringUtils.isNotEmpty(map.get(p)[0])){
                    return false;
                }
            }
            return true;
        }else {
            return true;
        }
    }
}
